import java.util.ArrayList;


public class DataStreamer {
	
	
	public static void main(String args[]){
		Integer[] array10 = generateUnsortedArray(10, -1000000, 1000000);
		Integer[] array10insertion = array10;
		Integer[] array10quicksort = array10;
		Integer[] array10mergesort = array10;
		Integer[] array100 = generateUnsortedArray(100, -1000000, 1000000);
		Integer[] array100insertion = array100;
		Integer[] array100quicksort = array100;
		Integer[] array100mergesort = array100;
		Integer[] array1000 = generateUnsortedArray(1000, -1000000, 1000000);
		Integer[] array1000insertion = array1000;
		Integer[] array1000quicksort = array1000;
		Integer[] array1000mergesort = array1000;
		Integer[] array10000 = generateUnsortedArray(10000, -1000000, 1000000);
		Integer[] array10000insertion = array10000;
		Integer[] array10000quicksort = array10000;
		Integer[] array10000mergesort = array10000;
		Integer[] array100000 = generateUnsortedArray(100000, -1000000, 1000000);
		Integer[] array100000insertion = array100000;
		Integer[] array100000quicksort = array100000;
		Integer[] array100000mergesort = array100000;
		
		System.out.println("10");
		testInsertionSort(array10insertion);
		testMergeSort(array10mergesort);
		testQuickSort(array10quicksort);
		System.out.println("100");
		testInsertionSort(array100insertion);
		testMergeSort(array100mergesort);
		testQuickSort(array100quicksort);
		System.out.println("1000");
		testInsertionSort(array1000insertion);
		testMergeSort(array1000mergesort);
		testQuickSort(array1000quicksort);
		System.out.println("10000");
		testInsertionSort(array10000insertion);
		testMergeSort(array10000mergesort);
		testQuickSort(array10000quicksort);
		System.out.println("100000");
		testInsertionSort(array100000insertion);
		testMergeSort(array100000mergesort);
		testQuickSort(array100000quicksort);


		
		
		
	}
	
	
	public static Integer[] generateUnsortedArray(int size,int minVal, int maxVal){
		int Variation = maxVal-minVal;
		
		ArrayList<Integer> list = new ArrayList<Integer>();
		for(int i = 0; i < size; i++){
			list.add((int)(Math.random()*Variation)+minVal);
		}
		Integer[] array = new Integer[list.size()];
		list.toArray(array);
		return array;
	}
	
	public static void invert(Object[] array){
		for(int i = 0; i < array.length/2; i++){
			verruil(array, i, array.length-(i+1));
		}
	}
	
	private static void verruil(Object[] array, int index1, int index2){
		Object s = array[index1];
		array[index1] = array[index2];
		array[index2] = s;
	}
	
	private static void testQuickSort(Integer[] array){
		System.out.println("unsorted");
		long starttime = System.currentTimeMillis();
		QuickSort.sort(array);
		System.out.println("time past: " + (System.currentTimeMillis()-starttime));
		System.out.println("sorted");
		starttime = System.currentTimeMillis();
		QuickSort.sort(array);
		System.out.println("time past: " + (System.currentTimeMillis()-starttime));
		invert(array);
		System.out.println("reversed sorted");
		starttime = System.currentTimeMillis();
		QuickSort.sort(array);
		System.out.println("time past: " + (System.currentTimeMillis()-starttime));
	}
	
	private static void testMergeSort(Integer[] array){
		System.out.println("unsorted");
		long starttime = System.currentTimeMillis();
		array = (Integer[]) MergeSort.sort(array);
		System.out.println("time past: " + (System.currentTimeMillis()-starttime));
		System.out.println("sorted");
		starttime = System.currentTimeMillis();
		array = (Integer[]) MergeSort.sort(array);
		System.out.println("time past: " + (System.currentTimeMillis()-starttime));
		invert(array);
		System.out.println("reversed sorted");
		starttime = System.currentTimeMillis();
		array = (Integer[]) MergeSort.sort(array);
		System.out.println("time past: " + (System.currentTimeMillis()-starttime));
	}
	
	private static void testInsertionSort(Integer[] array){
		System.out.println();
		System.out.println("unsorted");
		
		testArray(array);
		
		long starttime = System.currentTimeMillis();
		array = (Integer[]) InsertionSort.sort(array);
		System.out.println("time past: " + (System.currentTimeMillis()-starttime));
		
		System.out.println("sorted");
		

		testArray(array);

		starttime = System.currentTimeMillis();
		array = (Integer[]) InsertionSort.sort(array);
		System.out.println("time past: " + (System.currentTimeMillis()-starttime));
		
		invert(array);
		System.out.println("reversed sorted");
		

		testArray(array);

		starttime = System.currentTimeMillis();
		array = (Integer[]) InsertionSort.sort(array);
		System.out.println("time past: " + (System.currentTimeMillis()-starttime));
		testArray(array);
	}
	
	public static void testArray(Comparable[] array){
		for(int i = 0; i < array.length-1; i++){
			if(array[i].compareTo(array[i+1]) == 1){
				System.out.println("Array is not sorted");
				return;
			}
		}
		System.out.println("Array is sorted correctly");
	}
	
}
